# log.py
# 闭包

from time import ctime,sleep

def logged(when):
    def log(f, *args, **kargs):
        print('funciton,%s args,%r kargs %r' % (f,args,kargs))

    def pre_logged(f):
        def wrapper(*args, **kargs):
            log(f, *args, **kargs)
            return f(*args, **kargs)
        return wrapper

    def post_logged(f):
        def wrapper(*args, **kargs):
            now = ctime()
            print('start time: %s'% now)
            try:
                return f(*args, **kargs)
            finally:
                log(f, *args, **kargs)
                print("end time: %s" % (ctime()))
        return wrapper

    try:
        return {"pre": pre_logged, "post": post_logged}[when]
    except KeyError as e:
        print(e)

@logged('post')
def hello(name):
    print('hello')
    sleep(1)
    print(name)

hello('liuyu')

